﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.Odbc;

namespace lp_ge_gt
{
    class dbConn
    {
        private connType ct;
        public enum connType { saleSystem, shop };
        public dbConn(connType ct)
        {
            this.ct = ct;
        }
        public String[][] query(String que)
        {
            List<String[]> retList = new List<String[]>();
            List<String> subRetList = new List<String>();
            OdbcDataReader dr = ask(que);
            while (dr.Read())
            {
                for (int i = 0; i < dr.FieldCount; i++)
                {
                    subRetList.Add(dr[i].ToString());
                }
                retList.Add(subRetList.ToArray());
                subRetList.Clear();
            }
            dr.Close();
            return retList.ToArray();

        }
        public void query(String que, Boolean ret)
        {
            ask(que, false);
        }
        public void query(String[] que, Boolean ret)
        {
            askMulti(que);
        }
        public String[] queryFirst(String que) 
        {
            List<String> retList = new List<String>();
            OdbcDataReader dr =  ask(que);
            while (dr.Read())
            {
                retList.Add(dr[0].ToString());
            }
            dr.Close();
            return retList.ToArray();
        }
        private OdbcDataReader ask(String que)
        {
            return ask(que,true);
        }
        private void askMulti(String[] que)
        {
            String ex_sum = "";
            String connStr = "";
            if (ct == connType.saleSystem)
            {
                connStr = "DSN=IGT";
            }
            if (ct == connType.shop)
            {
                connStr = "DSN=GSA";
            }
            OdbcConnection DbConnection = new OdbcConnection(connStr);
            DbConnection.Open();
            for (int i = 0; i < que.Length; i++)
            {
                try
                {
                    OdbcCommand DbCommand = DbConnection.CreateCommand();
                    DbCommand.CommandText = que[i];
                    OdbcDataReader DbReader = DbCommand.ExecuteReader();
                    DbReader.Close();
                    DbCommand.Dispose();
                }
                catch (Exception ex)
                {
                    ex_sum +=  " que:"+i.ToString()+"msg: "+ ex.Message + " \n\r ";
                }
            }
            DbConnection.Close();
            if (ex_sum != "")
            {
                throw new Exception(ex_sum);
            }
        }
        private OdbcDataReader ask(String que, Boolean ret)
        {
            String connStr = "";
            if (ct == connType.saleSystem)
            {
                connStr="DSN=IGT";
            }
            if (ct == connType.shop)
            {
                connStr = "DSN=GSA";
            }
            OdbcConnection DbConnection = new OdbcConnection(connStr);
            DbConnection.Open();
            OdbcCommand DbCommand = DbConnection.CreateCommand();
            DbCommand.CommandText = que;
            OdbcDataReader DbReader = DbCommand.ExecuteReader();
           
            if (!ret)
            {
                DbReader.Close();
                DbCommand.Dispose();
                DbConnection.Close();
                return null;
            }
            else
            {
                DbCommand.Dispose();
                DbConnection.Close();
                return DbReader;
            }
            return null;
        }
    }
}
